/** @license
 * MIT License
 *
 * Copyright (c) 2018
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
import React from 'react'
import { View, Checkbox, Label, CheckboxGroup, Text } from '@tarojs/components'
import classnames from 'classnames'

import TodoTextInput from '../TodoTextInput/TodoTextInput'
import './TodoItem.scss'

export default class TodoItem extends React.Component {
  static defaultProps = {
    todo: {}
  }

  state = {
    editing: false
  }

  handleSubmit = () => {
    const val = this.state.editText.trim()
    if (val) {
      this.props.onSave(val)
      this.setState({editText: val})
    } else {
      this.props.onDestroy()
    }
  }

  handleDoubleClick = () => {
    this.setState({editing: true})
  }

  handleSave = (id, text) => {
    console.log('handleSave', id, text)
    if (text?.length === 0) {
      this.props.onDeleteTodo(id)
    } else {
      this.props.onEditTodo(text)
    }
    this.setState({editing: false})
  }

  handleCompleteTodo = (todo) => {
    console.log('handleCompleteTodo', todo)
    this.props.onCompleteTodo(todo.id)
  }

  render () {
    const {todo} = this.props

    let element
    if (this.state.editing) {
      element = (
        <TodoTextInput text={todo.text} editing={this.state.editing} onSave={this.handleSave.bind(this, todo.id)} />
      )
    } else {
      element = (
        <View className='view'>
        <CheckboxGroup id={`todo-checkbox-${todo.id}`} onChange={this.handleCompleteTodo.bind(this, todo)}>
          <Label className='checkbox-label'>
            <Checkbox groupId={`todo-checkbox-${todo.id}`} className='checkbox' checked={todo.completed} />
            <Text style={{color: '#4d4d4d'}}>{todo.text}</Text>
          </Label>
        </CheckboxGroup>
        </View>
      )
    }

    return (
      <View className={classnames({
        'list-item': true,
        completed: todo.completed,
        editing: this.state.editing
      })}
      >
        {element}
      </View>
    )
  }
}
